package com.enseirb.bdxmap.converter;

import java.util.ArrayList;
import java.util.List;

import com.enseirb.bdxmap.dao.dto.AbstractMarkerDTO;
import com.enseirb.bdxmap.exception.BdxMapException;
import com.enseirb.bdxmap.service.model.AbstractMarkerBO;

public abstract class AbstractMarkerConverter<T extends AbstractMarkerDTO, U extends AbstractMarkerBO>
		implements MarkerConverter {

	public List<U> getData() throws BdxMapException {
		List<T> serviceDTOs = getDataFromDAO();
		List<U> serviceBOs = new ArrayList<U>();
		for (T serviceDTO : serviceDTOs) {
			U currentBO = serviceDTOToBO(serviceDTO);
			serviceBOs.add(currentBO);
		}
		return serviceBOs;
	}

	protected abstract List<T> getDataFromDAO() throws BdxMapException;

	protected U serviceDTOToBO(T serviceDTO) {
		U serviceBO = newMarkerBO();
		serviceBO.id = serviceDTO.id;
		serviceBO.name = serviceDTO.name;
		serviceBO.latitude = Double.valueOf(serviceDTO.latitude.replaceAll(",",
				"."));
		serviceBO.longitude = Double.valueOf(serviceDTO.longitude.replaceAll(
				",", "."));
		serviceBO = setCustomFieldsBO(serviceDTO, serviceBO);
		return serviceBO;
	}

	protected abstract U newMarkerBO();

	protected abstract U setCustomFieldsBO(T serviceDTO, U serviceBO);
}